<template>
  <div class="content-wraper">
    <div class="search-box">
      <el-button type="success" icon="el-icon-plus" @click="addClassify('same')">同级新增</el-button>
      <el-button type="success" icon="el-icon-plus" @click="addClassify('next')">下级新增</el-button>
      <!-- <el-button type="danger" @click="addSecurityTrain()">删除</el-button> -->
    </div>
    <div class="detail">
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="dataList"
        :columns="columns"
        :total="total"
        :no-page="true"
        row-key="id"
        :tree-props="{children: 'nodes', hasChildren: 'hasChildren'}"
        @pageChange="pageChange"
      >
        <template v-slot:name="{row}">
          <a class="com-link" @click="viewDetail(row)">{{ row.name }}</a>
        </template>
        <template v-slot:options="{row}">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editSecurityTrain(row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="delSecurityTrain(row)">删除</el-button>
        </template>
        <template v-slot:trainInOut="{row: {trainInOut}}">
          {{ getDictName(securityTrainTrainInOut, trainInOut) }}
        </template>
        <template v-slot:startTime="{row: {startTime}}">
          {{ dateFormat(startTime) }}
        </template>
        <template v-slot:endTime="{row: {endTime}}">
          {{ dateFormat(endTime) }}
        </template>
        <template v-slot:certificate="{row: {certificate}}">
          {{ getDictName(securityTrainCertificate, certificate) }}
        </template>
        <template v-slot:createTime="{row: {createTime}}">
          {{ dateFormat(createTime) }}
        </template>

      </com-table>
    </div>
    <addHiddenDangerClassify
      :show-modal.sync="showModal"
      :opt-type="optType"
      :add-level="addLevel"
      :relate-data="selectRow"
      :data="unitDetail"
      @freshList="search"
    />
  </div>
</template>

<script>
import { deleteSecurityRiskCategory, getSecurityRiskCategoryTree } from '@/api/securityManage'
import moment from 'moment'
import { getDictName } from '@/utils/comm'
import { securityTrainTrainInOut, securityTrainCertificate } from '@/utils/dicData'
import addHiddenDangerClassify from './addHiddenDangerClassify'
export default {
  name: 'SecurityTrain',
  components: {
    addHiddenDangerClassify
  },
  data() {
    return {
      name: '',
      addLevel: '',
      selectRow: null,
      getDictName,
      securityTrainTrainInOut,
      securityTrainCertificate,

      dataList: [],
      total: 0,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      showModal: false,
      optType: 'add',
      unitDetail: {},
      columns: [
        {
          label: '隐患分类编号',
          prop: 'number'
        },
        {
          label: '隐患分类名称',
          prop: 'name'
        },
        {
          label: '是否禁用',
          prop: 'enable'
        },
        {
          label: '说明',
          prop: 'description'
        },
        {
          fixed: 'right',
          label: '操作',
          width: '180',
          prop: 'options',
          slot: 'options'
        }
      ]
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    addClassify(level) {
      this.addLevel = level
      this.showModal = true
    },
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    pageChange(data) {
      this.pageParams = data
      this.getDataList()
    },
    getDataList() {
      // const params = {
      //   ...this.pageParams
      // }
      getSecurityRiskCategoryTree().then(res => {
        const { nodes } = res.data || {}
        if (nodes) {
          this.dataList = nodes
        }
        // if (data) {
        //   this.dataList = data
        //   this.total = total - 0
        // }
      })
    },
    addSecurityTrain() {
      this.optType = 'add'
      this.showModal = true
    },
    editSecurityTrain(data) {
      this.optType = 'edit'
      this.unitDetail = data
      this.showModal = true
    },
    viewDetail(data) {
      this.optType = 'detail'
      this.unitDetail = data
      this.showAddFileModal = true
    },
    delSecurityTrain(data) {
      this.$confirm('确定要删除该安全隐患分类吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const { id } = data
        deleteSecurityRiskCategory({
          securityRiskCategoryId: id
        }).then(res => {
          const { code, msg } = res
          if (code === '0001') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getDataList()
          } else {
            this.$message({
              type: 'error',
              message: msg || '删除失败!'
            })
          }
        })
      }).catch(() => {
      })
    },
    search() {
      // this.pageParams.pageNum = 1
      this.getDataList()
    },
    reset() {

    }

  }
}
</script>

<style lang="scss" scoped>
.search-box {
  height: 50px;
}
.detail{
  height: calc(100% - 50px);
}
</style>
